package com.gxc.datacore.metadata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.datacore.metadata.domain.Table;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service
public class TableService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;
	@Autowired
	private ColumnService columnService;

	public SqlDao getSqlDao() {
		return sqlDao;
	}

	public void setSqlDao(SqlDao sqlDao) {
		this.sqlDao = sqlDao;
	}

	public BeanService getEntityService() {
		return beanService;
	}

	public void setEntityService(BeanService beanService) {
		this.beanService = beanService;
	}

	public ColumnService getColumnService() {
		return columnService;
	}

	public void setColumnService(ColumnService columnService) {
		this.columnService = columnService;
	}

	public Table retrieve(String id) {
		return beanService.get(sqlDao, Table.class, id);
	}

	public int create(Table table) {
		return beanService.create(sqlDao, table);
	}

	public int update(Table table) {
		return beanService.update(sqlDao, table);
	}

	public int delete(String id) {
		columnService.deleteByTableId(id);
		return beanService.delete(sqlDao, Table.class, id);
	}

	public int delete(String[] id_array) {
		int i = 0;
		for (String id : id_array) {
			i += this.delete(id);
		}
		return i;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int deleteByDatabaseId(String database_id) {
		Map params = new HashMap();
		params.put("database_id", database_id);
		List tableList = this.list(params);
		int num = 0;
		for (int i = 0; i < tableList.size(); i++) {
			Table table = (Table) tableList.get(i);
			num += this.delete(table.getId());
		}
		return num;
	}

	@SuppressWarnings("rawtypes")
	public List list(Map parameter) {
		return sqlDao.selectList("datacore.table.list", parameter);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map parameter, PageInfo pageInfo) {
		return sqlDao
				.selectPageList("datacore.table.list", parameter, pageInfo);
	}

	/**
	 * 查询角色有权访问的表
	 * 
	 * @param parameter
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List listByRoles(Map parameter) {
		return sqlDao.selectList("datacore.table.listByRoles", parameter);
	}

	/**
	 * 查询角色有权访问的表
	 * 
	 * @param parameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList listPageByRoles(Map parameter, PageInfo pageInfo) {
		return sqlDao.selectPageList("datacore.table.listByRoles", parameter,
				pageInfo);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Table retrieveByName(String database_id, String schema_name,
			String table_name) {
		Map params = new HashMap();
		params.put("database_id", database_id);
		params.put("schema_name", schema_name);
		params.put("table_name", table_name);
		return sqlDao.selectOne("datacore.table.retrieveByName", params);
	}
}
